package xyz.ranyh.jobclient;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import xyz.ranyh.jobclient.http.ModifyPasswodTask;
import xyz.ranyh.jobclient.util.Application;

public class ModifyPasswdActivity extends BaseActivity {

    EditText mOldPassEdit;
    EditText mNewPassEdit;
    EditText mConfirmPassEdit;
    Button mModifyButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_passwd);
        getSupportActionBar().setTitle(R.string.title_activity_modify_password);

        mOldPassEdit = (EditText) findViewById(R.id.old_password_edit);
        mNewPassEdit = (EditText) findViewById(R.id.new_password_edit);
        mConfirmPassEdit = (EditText) findViewById(R.id.c_password_edit);
        mModifyButton = (Button) findViewById(R.id.modify_button);

        mModifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oPassword = mOldPassEdit.getText().toString();
                String nPassword = mNewPassEdit.getText().toString();
                String cPassword = mConfirmPassEdit.getText().toString();

                if (oPassword.isEmpty() || nPassword.isEmpty() || cPassword.isEmpty()) {
                    showToast("不能留空哦！");
                    return;
                }

                if (nPassword.length() < 5) {
                    showToast("新密码太短了！");
                    return;
                }

                if (!nPassword.equals(cPassword)) {
                    showToast("两次密码不正确！");
                    return;
                }


                new ModifyPasswodTask(getBaseContext(), nPassword, oPassword) {
                    @Override
                    protected void onPostExecute(final JSONObject success) {
                        if (success != null) {
                            try {
                                Toast.makeText(ModifyPasswdActivity.this, success.getString("message"), Toast.LENGTH_SHORT).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.execute();
            }
        });
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

    }
}
